package com.ruoyi.smartai.service.impl;

import com.ruoyi.smartai.service.IDynamicQuartzService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.stereotype.Service;


@Service
public class DynamicQuartzServiceImpl implements IDynamicQuartzService {


    private static final Logger log = LoggerFactory.getLogger(DynamicQuartzServiceImpl.class);

    @Autowired
    private Scheduler scheduler;

    /**
     * 动态添加任务
     *
     * @param jobName        任务名称
     * @param jobGroup       任务组
     * @param triggerName    触发器名称
     * @param triggerGroup   触发器组
     * @param jobClass       任务类
     * @param cronExpression Cron 表达式
     * @param jobDataMap     任务参数
     */
    @Override
    public void addJob(String jobName, String jobGroup, String triggerName, String triggerGroup,
                       Class<? extends Job> jobClass, String cronExpression, JobDataMap jobDataMap) {
        try {
            // 定义任务
            JobDetail jobDetail = JobBuilder.newJob(jobClass)
                    .withIdentity(jobName, jobGroup)
                    .usingJobData(jobDataMap)
                    .build();
            // 定义触发器
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerName, triggerGroup)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
                    .build();
            // 调度任务
            scheduler.scheduleJob(jobDetail, trigger);
            log.info("添加 " + jobName + " 任务成功:" + cronExpression);
        } catch (Exception e) {
            log.error("addJob error:" + e);
        }
    }

    @Override
    public boolean checkJobExists(String jobName, String jobGroup) {
        try {
            // 根据 jobName 和 jobGroup 创建 JobKey
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            // 检查任务是否存在
            return scheduler.checkExists(jobKey);
        } catch (SchedulerException e) {
            // 处理异常
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 动态删除任务
     *
     * @param jobName  任务名称
     * @param jobGroup 任务组
     */
    @Override
    public void deleteJob(String jobName, String jobGroup) {
        try {
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            scheduler.deleteJob(jobKey);
        } catch (Exception e) {
            log.error("deleteJob error:" + e);
        }
    }

    /**
     * 动态更新任务
     *
     * @param jobName        任务名称
     * @param jobGroup       任务组
     * @param triggerName    触发器名称
     * @param triggerGroup   触发器组
     * @param cronExpression Cron 表达式
     */
    @Override
    public void updateJob(String jobName, String jobGroup, String triggerName, String triggerGroup,
                          String cronExpression) {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            Trigger newTrigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerKey)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
                    .build();
            scheduler.rescheduleJob(triggerKey, newTrigger);

        } catch (Exception e) {
            log.error("updateJob error:" + e);
        }
    }
}
